/**
 * Created by DevilsEyes on 2016/2/13.
 */
'use strict';

var express = require('express');
var router = express.Router();

var $homework = require('../midware/homework');
var $session = require('../midware/session');
var $file = require('../midware/file');

let filter = (h, studentId)=>({
    _id: h._id,
    title: h.title,//标题

    description: h.description || '',//描述

    files: h.files.filter((i)=>i.id == studentId),//文件 文件JSON字符串组

    overtime: h.overtime,//截止时间
    created: h.created//发布时间
});

//获取相关作业列表
router.get('/list', function (req, res, next) {

    let ssid = req.cookies.ssid;
    if (ssid === undefined) {
        return res.json({
            code: 9999,
            msg: '未登录'
        });
    } else {
        var Se = $session.getBySid(ssid);
        if (Se.role >= 999) {
            $homework
                .getList()
                .then((homeworks)=> {

                    res.json({
                        code: 0,
                        count: homeworks.length,
                        list: homeworks.map((h)=>filter(h, Se.studentId))
                    });

                });
        } else {
            $homework
                .getList(Se.class)
                .then((homeworks)=> {

                    res.json({
                        code: 0,
                        count: homeworks.length,
                        list: homeworks.map((h)=>filter(h, Se.studentId))
                    });

                });
        }
    }
});

//获取某次作业信息
router.get('/', function (req, res, next) {

    let id = req.query._id;
    let ssid = req.cookies.ssid;

    $homework
        .getOnebyId(id)
        .then((homework)=> {

            var Se = $session.getBySid(ssid);

            res.json({
                code: 0,
                info: filter(homework, Se.studentId)
            });
        })
        .catch((e)=> {
            console.log(e);
            res.json({
                code: 9999,
                msg: '接口发生错误'
            });
        })
});

router.post('/', function (req, res, next) {
    try {

        let ssid = req.cookies.ssid;

        let data = JSON.parse(req.body.json);

        var Se = $session.getBySid(ssid);

        let homeworkId = data._id,
            studentId = Se.studentId,
            file = data.file;

        file.created = new Date().getTime();
        file.id = studentId;

        new Promise((O, X)=> {
            O();
        })
            .then(()=> {

                if (!file.path || !file) {
                    throw {
                        code: 9999,
                        msg: '没有选择文件'
                    };
                }

                if (Se == null) {
                    throw {
                        code: 9999,
                        msg: '未登录'
                    };
                }

                return $file.updatedByPath(file.path, {
                    name: file.name,
                    use: true
                });

            })
            .then(()=> {
                return $homework.push(homeworkId, file);
            })
            .then(()=> {
                res.json({
                    code: 0
                });
            })
            .catch((e)=> {
                console.log(e);
                $file.deleteByPath(file.path)
                    .then(()=> {
                        res.json({
                            code: e.code,
                            msg: e.msg
                        });
                    });

            });

    } catch (e) {
        console.log(e);
        res.json({
            code: 9999,
            msg: '接口发生错误'
        });
    }
});

module.exports = router;